/*
 * GNode.java
 *
 * Created on 13 maart 2008, 14:18
 *
 * @author wijnand.schepens@hogent.be
 */
package model.graphic.objects;

import model.graphic.objects.addons.RemoveAddon;
import model.graphic.objects.addons.ShrinkAddon;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Stroke;
import java.io.Serializable;
import java.util.ArrayList;
import model.graphic.locations.AbsoluteLocation;
import model.graphic.locations.Location;
import view.GUI.LabelReader;
import view.GUI.graphic.SkinInfo;
import view.GUI.graphic.SkinReader;

public class GNode extends RectangleGraphical implements Serializable {

    private String value = null;
    private ArrayList<GConnection> conns = new ArrayList<GConnection>();
    private int previousMouseX;
    private int previousMouseY;

    public GNode() {
        super(null, 100, 50);
    }

    public GNode(Location location, int x, int y) {
        super(location, x, y);
    }

    public GNode(String value) {
        this();
        this.value = value;
    }

    @Override
    public GNode clone() {
        GNode g = (GNode) super.clone();
        g.setRemoveAddon(new RemoveAddon());
        g.setShrinkAddon(new ShrinkAddon());
        g.setResizer(null);
        return g;
    }

    @Override
    public void setName(String name) {
        String toolTipText = this.getToolTipText();
        super.setName(name);
        this.setToolTipText(toolTipText);
    }

    @Override
    public void setType(String type) {
        super.setType(type);
        this.setToolTipText(LabelReader.getInstance().getHTMLString("info"+type));
    }

    @Override
    public void paint(Graphics2D g) {
        if (this.isVisible()) {
            SkinInfo info = SkinReader.getInstance().getSkinInfo(LabelReader.noTemplate(type));
            Color oldColor = g.getColor();
            Stroke oldStroke = g.getStroke();
            getRemoveAddon().setCenter(new AbsoluteLocation(lowerRight.getX() - 10, lowerRight.getY() - 10));
            getShrinkAddon().setCenter(location);

            this.drawSkin(info, g);
            
            if (highlighted || selected) {
                g.setColor(highlightColor);
            } else {
                g.setColor(baseColor);
            }

            g.setStroke(new BasicStroke(2));
            g.drawRect(location.getX(), location.getY(), getWidth(), getHeight());
            g.setStroke(new BasicStroke(1));
            g.drawLine(location.getX(), location.getY() + getHeight() / 3, location.getX() + getWidth(), location.getY() + getHeight() / 3);
            if (value != null) {
                g.drawLine(location.getX(), location.getY() + getHeight() / 3 * 2, location.getX() + getWidth(), location.getY() + getHeight() / 3 * 2);
            }

            g.setFont(info.getFont());
            g.setColor(info.getFontColor());

            if (name != null) {
                if (getWidth() / 10 + 1 <= name.length()) {
                    drawCentered(g, name.substring(0, getWidth() / 10) + "...", new Rectangle(location.getX(), location.getY(), getWidth(), getHeight() / 3));
                } else {
                    drawCentered(g, name, new Rectangle(location.getX(), location.getY(), getWidth(), getHeight() / 3));
                }
            }

            if (type != null) {
                if (getWidth() / 10 + 1 <= type.length()) {
                    drawCentered(g, type.substring(0, getWidth() / 10) + "...", new Rectangle(location.getX(), location.getY() + getHeight() / 3, getWidth(), getHeight() / 3));
                } else {
                    drawCentered(g, type, new Rectangle(location.getX(), location.getY() + getHeight() / 3, getWidth(), getHeight() / 3));
                }
            }

            if (value != null) {
                if (getWidth() / 10 + 1 <= value.length()) {
                    drawCentered(g, value.substring(0, getWidth() / 10) + "...", new Rectangle(location.getX(), location.getY() + getHeight() / 3, getWidth(), getHeight() / 3));
                } else {
                    drawCentered(g, value, new Rectangle(location.getX(), location.getY() + getHeight() / 3 * 2, getWidth(), getHeight() / 3));
                }
            }

            g.setStroke(new BasicStroke(4.0f));
            if (!getShrinkAddon().isAddonHighlighted()) {
                g.setStroke(oldStroke);
                g.setColor(Color.RED);
            } else {
                g.setStroke(new BasicStroke(4.0f));
                g.setColor(Color.BLUE);
            }

            if (!getShrinkAddon().isShrinked()) {
                g.draw(getShrinkAddon().getL1());

            } else {
                g.draw(getShrinkAddon().getL1());
                g.draw(getShrinkAddon().getL2());
            }
            if (!getRemoveAddon().isAddonHighlighted()) {
                g.setStroke(oldStroke);
                g.setColor(Color.RED);
            } else {
                g.setStroke(new BasicStroke(4.0f));
                g.setColor(Color.BLUE);
            }
            g.draw(getRemoveAddon().getL1());
            g.draw(getRemoveAddon().getL2());
            g.setStroke(oldStroke);
            g.setColor(oldColor);
        }
    }

    public Point getValueLocation() {
        int x = location.getX();
        int y = location.getY();
        Rectangle bounds = new Rectangle(x, y + getHeight() / 3 * 2, getWidth(), getHeight() / 3);
        int xx = bounds.x + bounds.width / 2 - (int) (name.length() / 2);     // center
        int yy = bounds.y + bounds.height - (int) 10 / 2 - 10;
        return new Point(xx, yy);
    }

    /**
     * @return the value
     */
    public String getValue() {
        return value;
    }

    /**
     * @param value the value to set
     */
    public void setValue(String value) {
        this.value = value;
    }

    public ArrayList<GConnection> getConns() {
        return conns;
    }

    public void addConnectionToNode(GConnection c) {
        conns.add(c);
    }

    public void removeConnectionToNode(GConnection c) {
        conns.remove(c);
    }

}





